package cn.kxm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.kxm.pojo.Area;
import cn.kxm.pojo.Company;
import cn.kxm.pojo.Companydetail;
import cn.kxm.service.AreaService;
import cn.kxm.service.CompanyService;
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController{
/*
 * 查询供应商并分页	
 */
	@Autowired
	private CompanyService companyService;
	@Autowired
	private AreaService areaService;
	@RequestMapping("getCompanyList")
	@ResponseBody
	public Object getCompanyList(Company company,Integer page,Integer rows){
		List<Company>list=companyService.getCompanyAll(company, rows, page);
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("total",companyService.getCompanyCountt(company));
		map.put("rows", list);
		return JSON.toJSON(map);
	}
	@RequestMapping("getAreaByPid")
	@ResponseBody
	public Object getAreaByPid(Integer pid){
		if(pid==null || pid==0){
			pid=0;
		}
		List<Area>list=areaService.getAreaByPid(pid);
		return JSON.toJSON(list);
	}
	@RequestMapping("getCompany_Detiail")
	@ResponseBody
	public Object getCompany_Detiail(Company company,Integer page,Integer rows){
		List<Map<String, Object>>list=companyService.getCompany_Detiail(company, rows, page);
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("total",companyService.getCompanyCounttt(company));
		map.put("rows", list);
		return JSON.toJSON(map);
	}
	
	@RequestMapping("getCompanydetailByCode")
	public String getCompanydetailByCode(Model model,String code){
		if(code!=null && code!=""){
			Map<String, Object>list=companyService.getCompanydetailsByCode(code);
			model.addAttribute("cdList",list);
			model.addAttribute("cityList",areaService.getAreaByPid((Integer) list.get("province")));
			return "web/goods/showCompanyDetiailNoUpdate";
		}
		return null;
	}
	@RequestMapping("getCompanydetailByCodeUpdate")
	public String getCompanydetailByCodeUpdate(Model model,String code){
		if(code!=null && code!=""){
			Map<String, Object>list=companyService.getCompanydetailsByCode(code);
		/*	if(areaService.getAreaByPid((Integer) list.get("province")).size()==0){
				model.addAttribute("cityList",areaService.getAreaById((Integer) list.get("province")));
			}else {
			}*/
			model.addAttribute("cityList",areaService.getAreaByPid((Integer) list.get("province")));
			model.addAttribute("cdList",list);
			return "web/goods/updateCompanyDetiail";
		}
		return null;
	}
	
	@RequestMapping("addCompany_detail")
	@ResponseBody
	public Object addCompany_detail(String code,Companydetail companydetail,Company company){
		Map<String, Object>map=new HashMap<String, Object>();
		boolean flag=false;
		if(code!=null && company!=null && companydetail!=null){
			int a=companyService.addCompany(company);
			if(a>0){
				companydetail.setC_code(code);
				int b=companyService.addCompanydetail(companydetail);
				if(b>0){
					flag=true;
				}
			}else{
				map.put("flag", "n");
			}
		}else{
			map.put("flag", "n");
		}
		if(flag){
			map.put("flag", "y");
		}
		return JSON.toJSON(map);
	}
	@RequestMapping("getCompany_Name")
	@ResponseBody
	public Object getCompany_Name(String  name){
		System.out.println(name);
		Company company=new Company();
		company.setName(name);
		int i=companyService.getCompanyCountt(company);
		if(i>0){
			return JSON.toJSONString("n");
		}else{
			return JSON.toJSONString("y");
		}
	}
	@RequestMapping("updateCompany_detail")
	@ResponseBody
	public Object updateCompany_detail(String code,Companydetail companydetail,Company company){
		Map<String, Object>map=new HashMap<String, Object>();
		boolean flag=false;
		if(company!=null && companydetail!=null){
			int a=companyService.modifyCompany(company);
			if(a>0){
				companydetail.setC_code(code);
				int b=companyService.modifyCompanydetail(companydetail);
				if(b>0){
					flag=true;
				}
			}else{
				map.put("flag", "n");
			}
		}else{
			map.put("flag", "n");
		}
		if(flag){
			map.put("flag", "y");
		}
		return JSON.toJSON(map);
	}
	
	@RequestMapping("delCompany_detail")
	@ResponseBody
	public Object delCompany_detail(String code){
		Map<String, Object>map=new HashMap<String, Object>();
		boolean flag=false;
		if(code!=null && code!=""){
			int b=companyService.delCompanydetail(code);
			if(b>0){
				int a=companyService.delCompany(code);
				if(a>0){
					flag=true;
				}
			}else{
				map.put("flag", "n");
			}
		}else{
			map.put("flag", "n");
		}
		if(flag){
			map.put("flag", "y");
		}
		return JSON.toJSON(map);
	}
}
